package cn.fboost.mtos.controller;

import cn.fboost.mtos.annotation.IgnoreTokenCheck;
import cn.fboost.mtos.annotation.RoleIs;
import cn.fboost.mtos.dto.Response;
import cn.fboost.mtos.dto.internal.Session;
import cn.fboost.mtos.dto.request.*;
import cn.fboost.mtos.service.ProductService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;

/**
 * 商品控制器
 */
@RestController
@RequestMapping("product")
public class ProductController {
    private final ProductService productService;

    public ProductController(ProductService productService) {
        this.productService = productService;
    }

    /**
     * 获取商品列表
     *
     * @param request {@link PageRequest}
     * @return {@link Response}
     */
    @IgnoreTokenCheck
    @PostMapping("list")
    public Response listByCategory(@RequestBody @Validated PageRequest request) {
        return productService.list(request);
    }

    /**
     * 根据分类获取商品列表
     *
     * @param request {@link ProductListByCategoryRequest}
     * @return {@link Response}
     */
    @IgnoreTokenCheck
    @PostMapping("category/list")
    public Response listByCategory(@RequestBody @Validated ProductListByCategoryRequest request) {
        return productService.listByCategory(request);
    }

    /**
     * 根据月销量获取商品列表
     *
     * @param request {@link ProductListByMonthlySalesRequest}
     * @return {@link Response}
     */
    @IgnoreTokenCheck
    @PostMapping("monthlySales/list")
    public Response listByMonthlySales(@RequestBody @Validated ProductListByMonthlySalesRequest request) {
        return productService.listByMonthlySales(request);
    }

    /**
     * 根据商品id获取商品详情
     *
     * @param id 商品id
     * @return {@link Response}
     */
    @IgnoreTokenCheck
    @PostMapping("{id}/detail")
    public Response detailById(@PathVariable("id") @Validated @Min(value = 1, message = "商品id有误") Integer id) {
        return productService.detail(id);
    }

    /**
     * 获取某商品最近评价
     *
     * @param id 商品id
     * @return {@link Response}
     */
    @IgnoreTokenCheck
    @GetMapping("comments/{id}")
    public Response getProductComments(@PathVariable("id") @Validated @Min(value = 1, message = "商品id有误") Integer id) {
        return productService.getComments(id);
    }

    /**
     * 创建商品 - 管理员接口
     *
     * @param request {@link ProductCreateRequest}
     * @return {@link Response}
     */
    @RoleIs({Session.Role.ADMIN})
    @PostMapping("create")
    public Response create(@RequestBody @Validated ProductCreateRequest request) {
        return productService.create(request);
    }

    /**
     * 修改商品信息 - 管理员接口
     *
     * @param request {@link ProductUpdateRequest}
     * @return {@link Response}
     */
    @RoleIs({Session.Role.ADMIN})
    @PostMapping("update")
    public Response update(@RequestBody @Validated ProductUpdateRequest request) {
        return productService.update(request);
    }

    /**
     * 删除商品 - 管理员接口
     *
     * @param request {@link ProductDeleteRequest}
     * @return {@link Response}
     */
    @RoleIs({Session.Role.ADMIN})
    @PostMapping("delete")
    public Response delete(@RequestBody @Validated ProductDeleteRequest request) {
        return productService.delete(request);
    }

    @RoleIs({Session.Role.ADMIN})
    @GetMapping("count")
    public Response count() {
        return productService.count();
    }
}
